package cn.akwangl.java8.test.stream;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

/**
 * Stream 创建的四种方式
 * 1. Collection 方式 {@link StreamCreate#collectionTest()}
 * 2. {@link Stream#of(Object[])} 静态方法 {@link StreamCreate#ArraysTest()}
 * 3. {@link Stream#of(Object[])} 静态方法 方式 {@link StreamCreate#streamOfTest()}
 * 4. 无限流 {@link StreamCreate#iterateAndGenerateTest()}
 *
 * @author youyongkun
 * @date 2021/5/28 10:04
 */
public class StreamCreate {


    /**
     * Collection 方式 {@link Collection#stream()}
     *
     * @author youyongkun
     * @date 2021/5/28 10:06
     */
    @Test
    public void collectionTest() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        list.stream().forEach(System.out::println);
    }

    /**
     * Arrays静态方法 {@link Arrays#stream(Object[])}
     *
     * @author youyongkun
     * @date 2021/5/28 10:09
     */
    @Test
    public void ArraysTest() {
        Arrays.stream(new int[]{1, 2, 3, 4, 5, 6}).forEach(System.out::println);
    }

    /**
     * {@link Stream#of(Object[])} 静态方法
     *
     * @author youyongkun
     * @date 2021/5/28 10:11
     */
    @Test
    public void streamOfTest() {
        Stream.of(1, 2, 3, 4, 5, 6).forEach(System.out::println);
    }

    /**
     * 无限流
     * {@link Stream#iterate(Object, UnaryOperator)}
     * {@link Stream#generate(Supplier)}
     *
     * @author youyongkun
     * @date 2021/5/28 10:14
     */
    @Test
    public void iterateAndGenerateTest() {
        Stream.generate(new Random()::nextInt).limit(500).forEach(System.out::println);
        System.err.println("----------------------------------------------------");
        Stream.iterate(0, (n) -> n + 1).limit(500).forEach(System.out::println);
    }

}
